package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RcHtmlMapper;
import com.ruoyi.system.domain.RcHtml;
import com.ruoyi.system.service.IRcHtmlService;

/**
 * 内容Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-27
 */
@Service
public class RcHtmlServiceImpl implements IRcHtmlService
{
    @Autowired
    private RcHtmlMapper rcHtmlMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询内容
     *
     * @param id 内容主键
     * @return 内容
     */
    @Override
    public RcHtml selectRcHtmlById(Long id)
    {
        return rcHtmlMapper.selectRcHtmlById(id);
    }

    @Override
    public RcHtml selectRcHtml(RcHtml rcHtml) { return rcHtmlMapper.selectRcHtml(rcHtml); }

    /**
     * 查询内容列表
     *
     * @param rcHtml 内容
     * @return 内容
     */
    @Override
    public List<RcHtml> selectRcHtmlList(RcHtml rcHtml)
    {
        return rcHtmlMapper.selectRcHtmlList(rcHtml);
    }

    @Override
    public ArrayList<String> filterCate(RcHtml rcHtml)
    {
        List<SysDictData> list = dictDataMapper.selectDictDataByType("web_cate");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ArrayList<String> cateList = new ArrayList<String>();
        list.stream().forEach(item->{
            String remark = item.getRemark();
            if(!StringUtils.isEmpty(remark)){
                int i = String.format(",%s,",remark).indexOf(','+loginUser.getDeptId().toString()+',');
                if(i != -1){
                    cateList.add(item.getDictValue());
                }
            }else{
                //cateList.add(item.getDictValue());
            }
        });
        return cateList;
    }

    /**
     * 检索内容列表
     */
    @Override
    public List<RcHtml> searchRcHtmlList(RcHtml rcHtml)
    {
        return rcHtmlMapper.searchRcHtmlList(rcHtml);
    }

    /**
     * 新增内容
     *
     * @param rcHtml 内容
     * @return 结果
     */
    @Override
    public int insertRcHtml(RcHtml rcHtml)
    {
        rcHtml.setCreateTime(DateUtils.getNowDate());
        return rcHtmlMapper.insertRcHtml(rcHtml);
    }

    /**
     * 修改内容
     *
     * @param rcHtml 内容
     * @return 结果
     */
    @Override
    public int updateRcHtml(RcHtml rcHtml)
    {
        rcHtml.setUpdateTime(DateUtils.getNowDate());
        return rcHtmlMapper.updateRcHtml(rcHtml);
    }

    /**
     * 批量删除内容
     *
     * @param ids 需要删除的内容主键
     * @return 结果
     */
    @Override
    public int deleteRcHtmlByIds(Long[] ids)
    {
        return rcHtmlMapper.deleteRcHtmlByIds(ids);
    }

    /**
     * 删除内容信息
     *
     * @param id 内容主键
     * @return 结果
     */
    @Override
    public int deleteRcHtmlById(Long id)
    {
        return rcHtmlMapper.deleteRcHtmlById(id);
    }
}
